package com.mobzilla.activity;

import android.app.Activity;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.mobzilla.model.Bug;
import com.mobzilla.service.ServiceFactory;

public class BugProcessor extends Activity implements OnClickListener
{
	public void onCreate(Bundle b)
	{
		super.onCreate(b);
		
		setContentView(R.layout.processor);
		
		initListeners();
		
		initScrollbars();
	}
	
	private void initScrollbars()
	{
		TextView tv = (TextView)findViewById(R.id.resultsTextField);
		
		tv.setMovementMethod(new ScrollingMovementMethod());
	}
	
	private void initListeners()
	{
		Button searchButton = (Button)findViewById(R.id.searchButton);
		
		searchButton.setOnClickListener(this);
		
		Button closeButton = (Button)findViewById(R.id.closeButton);
		
		closeButton.setOnClickListener(new OnClickListener()
		{			
			@Override
			public void onClick(View v)
			{
				finish();
			}
		});
	}
	
	public void onClick(View v)
	{
		TextView resultsTextField = (TextView)findViewById(R.id.resultsTextField);
		
		TextView bugNumberTextField = (TextView)findViewById(R.id.bugNumberTextField);
		
		populateBugDetails(resultsTextField, bugNumberTextField);
	}
	
	private void populateBugDetails(TextView resultsTextField, TextView bugNumberTextField)
	{
		Integer bugNumber = Integer.valueOf(bugNumberTextField.getText().toString());
		
		Bug bug = ServiceFactory.getSearchServiceInstance().fetchDefectDetails(bugNumber);
		
		if (null != bug) 
		{
			resultsTextField.setText(bug.toString());
		}
		else
		{
			resultsTextField.setText(R.string.bugnotfound);
		}
	}
}
